/**
 * Created by zhangwx on 2015/10/15.
 */
var Points=[];
/**
 * 暂时解决地图第一次不显示的问题
 */

function initMapData(){
    /*var  heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20,"opacity":20,"gradient":
    {
        .2:'rgb(0, 139, 0)',
        .4:'rgb(192, 255,62)',
        .6:'rgb(238,238,0)',
        .8:'rgb(238, 118,0)',
        1.0:'rgb(255,0,0)'

    }});*/
    var  heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20});
   /* var  heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20,"gradient":
        {
        .2:'rgb(0, 255, 255)',
        .5:'rgb(0, 110, 255)',
        .8:'rgb(100, 0, 255)'
    }});*/
    map.clearOverlays();
    map.centerAndZoom("浙江省", 9);
    map.addOverlay(heatmapOverlay);
    heatmapOverlay.setDataSet({data:[],max:100});

}
/**
 *
 * @param data 数据
 * @param ops  配置
 */
function initData(data,ops){

    var  heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20});
   map.clearOverlays();

    var defaultOptions={
           type:"group",
           data:"data",
           lonb:"lng",
           latb:"lat",
           displayField:[],
           userInfo:"userInfo",
           cityCode:"cityName",
           max:100,//值 固定为10个等级
           count:"count",
           isRePlace:true //是否重新定位，默认true
    };
    var options = $.extend(defaultOptions,ops);


    // map.setZoom(map.getZoom()+8);
    if(data){
        if(options.type=="groupMap"){
            if(options.isRePlace){
                if(data[options.cityCode]){
                    map.centerAndZoom(data[options.cityCode]);
                    // getBoundary(data[options.cityCode]);
                }else{
                    //这要写默认值
                    map.centerAndZoom("浙江省", 9);
                    // getBoundary("浙江省");
                    //map.centerAndZoom(new BMap.Point(120.1674,30.286), 8);


                }

            }


            var points=[];
            if(data[options.data]){

                if(data[options.data].length>0){
                    points=data[options.data];
                }

              /*  for(var i=0;i<data[options.data].length;i++){
                   // Points.push(new BMap.Point(data[options.data][i][options.lonb],data[options.data][i][options.latb]));
                    points.push({"lng":data[options.data][i][options.lonb],"lat":data[options.data][i][options.latb],"count":(data[options.data][i][options.count])});
                }*/
            }


         //   map.setZoom(8);
            map.addOverlay(heatmapOverlay);
            heatmapOverlay.setDataSet({data:points,max:100});


         //   map.setZoom(8);
            heatmapOverlay.show();
        }else{
            if(data[options.data]) {


                for (var i = 0; i < data[options.data].length; i++) {
                    var content = "";
                    for (var f in options.displayField) {
                        content += options.displayField[f].label + ":" + data[options.data][i][options.displayField[f].value] + "\n";
                    }
                    var marker = new BMap.Marker(new BMap.Point(data[options.data][i][options.lonb], data[options.data][i][options.latb]), {title: content,icon:Icons.red});  // 创建标注
                    map.addOverlay(marker);
                    // 将标注添加到地图中
                     Points.push(new BMap.Point(data[options.data][i][options.lonb],data[options.data][i][options.latb]));
                }

            }
            if(options.isRePlace){
                if(Points.length>0){
                    map.setViewport(Points);
                }
            }



        }




    }
}

/**
 *在原基础上继续添加 主要用于多次添加不同类型点
 * @param data
 * @param ops
 * @param style(点样式)
 */
function AddData(data,ops,styles){


var style={

};
    var syl = $.extend(style,styles);
    var defaultOptions={
        type:"group",
        data:"data",
        lonb:"lng",
        latb:"lat",
        displayField:[],
        userInfo:"userInfo",
        cityCode:"cityName",
        max:100,//值 固定为10个等级
        count:"count"
    };
    var options = $.extend(defaultOptions,ops);
    /*if(data[options.cityCode]){
        map.centerAndZoom(data[options.cityCode]);
    }else{
        map.centerAndZoom("浙江省", 8);
    }*/
    if(data){
        if(data[options.data]) {

            for (var i = 0; i < data[options.data].length; i++) {
                var content = "";
                for (var f in options.displayField) {
                    content += options.displayField[f].label + ":" + data[options.data][i][options.displayField[f].value] + "\n";
                }
                syl.title = content;
                var marker = new BMap.Marker(new BMap.Point(data[options.data][i][options.lonb], data[options.data][i][options.latb]), syl);  // 创建标注
                map.addOverlay(marker);
                // 将标注添加到地图中
                Points.push(new BMap.Point(data[options.data][i][options.lonb],data[options.data][i][options.latb]));
            }

        }
        if(options.isRePlace) {
            if (Points.length > 0) {
                map.setViewport(Points);
            }
        }

    }
}

function AddUserAnalysisData(data,ops,styles){
	var style={

	};
	    var syl = $.extend(style,styles);
	    var defaultOptions={
	        type:"group",
	        data:"data",
	        lonb:"lng",
	        latb:"lat",
	        displayField:[],
	        userInfo:"userInfo",
	        cityCode:"cityName",
	        max:100,//值 固定为10个等级
	        count:"count"
	    };
	    var options = $.extend(defaultOptions,ops);
	    if(data){
	        if(data[options.data]) {

	            for (var i = 0; i < data[options.data].length; i++) {
	                var content = "";
	                for (var f in options.displayField) {
	                    content += options.displayField[f].label + ":" + data[options.data][i][options.displayField[f].value] + "\n";
	                }
	                syl.title = content;
	                var marker = new BMap.Marker(new BMap.Point(data[options.data][i][options.lonb], data[options.data][i][options.latb]), syl);  // 创建标注
	                map.addOverlay(marker);
	                // 将标注添加到地图中
	                Points.push(new BMap.Point(data[options.data][i][options.lonb],data[options.data][i][options.latb]));
	            }

	        }
	        if(options.isRePlace) {
	            if (Points.length > 0) {
	                map.setViewport(Points);
	            }
	        }

	    }
}
/**
 * 行政边界
 */
function getBoundary(name){
    var bdary = new BMap.Boundary();
    bdary.get(name, function(rs){       //获取行政区域
        map.clearOverlays();        //清除地图覆盖物
        var count = rs.boundaries.length; //行政区域的点有多少个
        if (count === 0) {
            alert('未能获取当前输入行政区域');
            return ;
        }
        var pointArray = [];
        for (var i = 0; i < count; i++) {
            var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
            map.addOverlay(ply);  //添加覆盖物
            pointArray = pointArray.concat(ply.getPath());
        }
        map.setViewport(pointArray);    //调整视野
        //执行完这里再执行其他，异步
    });
}


function layerLoad(){

    layer.load(2);

}

function noMapdata(){

    layer.closeAll('loading');
    layer.msg('没有查到地图数据！') ;
    setTimeout(function(){
        layer.closeAll('loading');
    }, 2000);
}

function closeLayer(){

        layer.closeAll('loading');

}